package cn.legend.travel.tour.team.dao.persist.repository.impl;

import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.util.PageInfoToPageDataConverter;
import cn.legend.travel.tour.team.dao.cache.ITourCacheRepository;
import cn.legend.travel.tour.team.dao.persist.mapper.TourMapper;
import cn.legend.travel.tour.team.dao.persist.repository.ITourRepository;
import cn.legend.travel.tour.team.pojo.po.TourDetailPO;
import cn.legend.travel.tour.team.pojo.po.TourPO;
import cn.legend.travel.tour.team.pojo.po.TourSearchPO;
import cn.legend.travel.tour.team.pojo.vo.TourDetailVO;
import cn.legend.travel.tour.team.pojo.vo.TourHotListVO;
import cn.legend.travel.tour.team.pojo.vo.TourHotVO;
import cn.legend.travel.tour.team.pojo.vo.TourVO;
import cn.legend.travel.tour.team.utils.TourPOToTourVOConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
@Slf4j
public class ITourRepositoryImpl implements ITourRepository {
    @Value("${legend.dao.default-query-page-size}")
    private Integer pageSize;
    @Autowired
    TourMapper tourMapper;
    @Autowired
    ITourCacheRepository tourCacheRepository;
    @Override
    public PageData<TourVO> listForGeneral(Integer pageNum) {
        log.debug("开始执行【综合排序】的数据访问，页码：{}",pageNum);
        System.out.println("页码：{}"+pageNum);
        PageHelper.startPage(pageNum,pageSize);
        List<TourPO> list = tourMapper.listForGeneral();
        List<TourVO> tourVOList = new ArrayList<>();
        for (TourPO tourPO : list) {
            tourVOList.add(TourPOToTourVOConverter.convert(tourPO));
        }
        PageInfo<TourVO> pageInfo = new PageInfo<>(tourVOList);

        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<TourVO> listForSaleCount(Integer pageNum) {
        log.debug("开始执行【销量排序】的数据访问，页码：{}",pageNum);
        PageHelper.startPage(pageNum,pageSize);
        List<TourPO> list = tourMapper.listForSaleCount();
        List<TourVO> tourVOList = new ArrayList<>();
        for (TourPO tourPO : list) {
            tourVOList.add(TourPOToTourVOConverter.convert(tourPO));
        }
        PageInfo<TourVO> pageInfo = new PageInfo<>(tourVOList);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<TourVO> listForPriceASC(Integer pageNum) {
        log.debug("开始执行【价格排序（小到大）】的数据访问，页码：{}",pageNum);
        PageHelper.startPage(pageNum,pageSize);
        List<TourPO> list = tourMapper.listForPriceASC();
        List<TourVO> tourVOList = new ArrayList<>();
        for (TourPO tourPO : list) {
            tourVOList.add(TourPOToTourVOConverter.convert(tourPO));
        }
        PageInfo<TourVO> pageInfo = new PageInfo<>(tourVOList);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<TourVO> listForPriceDesc(Integer pageNum) {
        log.debug("开始执行【价格排序（大到小）】的数据访问，页码：{}",pageNum);
        PageHelper.startPage(pageNum,pageSize);
        List<TourPO> list = tourMapper.listForPriceDesc();
        List<TourVO> tourVOList = new ArrayList<>();
        for (TourPO tourPO : list) {
            tourVOList.add(TourPOToTourVOConverter.convert(tourPO));
        }
        PageInfo<TourVO> pageInfo = new PageInfo<>(tourVOList);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public List<TourHotVO> listForHotItem(Integer pageSize) {
        log.debug("开始执行【热销商品】的数据访问，页码：{}",pageSize);
        return tourMapper.listForHotItem(pageSize);
    }

    @Override
    public PageData<TourSearchPO> listSearch(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询用于搜索的旅游团列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<TourSearchPO> list = tourMapper.listSearch();
        PageInfo<TourSearchPO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public TourDetailVO getItemByGuideId(Long guideId) {
        log.debug("开始执行【旅游团详情】的数据访问");
        TourDetailPO itemByGuideId = tourMapper.getItemByGuideId(guideId);
        return TourPOToTourVOConverter.DetailPO2DetailVOConvert(itemByGuideId);
    }
}
